package com.hqj.client.controller;

import com.hqj.client.entity.Comments;
import com.hqj.client.service.CommentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/comments")
@Api(tags = "博客评论接口")
public class CommentsController {

    //注入封装实体类对象
    @Resource
    private CommentsService commentsService;
    //新增一个对象
    @ApiOperation("插入一条数据")
    @PostMapping("/insert")
    public String insert1(@ApiParam("一个对象") @RequestBody Comments comments , Model model){
        Integer i = commentsService.insert1(comments);
        if(i > 0){
            model.addAttribute("model" , "添加成功");
            return "main";
        }else {
            model.addAttribute("model" , "添加失败");
            return "main";
        }
    }
    @ApiOperation("根据ID删除一条数据")
    @DeleteMapping("/delete")
    public String delete(Integer id , Model model){
        Integer i = commentsService.deleteById(id);
        if(i > 0){
            model.addAttribute("model","删除成功");
            return "main";
        }else {
            model.addAttribute("model","删除失败");
            return "main";
        }
    }
    @ApiOperation("根据ID查询一条数据")
    @GetMapping("/select")
    public String selectById(@ApiParam("这是一个ID") @RequestParam Integer id , Model model){
        Comments co = commentsService.selectById(id);
        if (co != null){
            model.addAttribute(co);
            System.out.println("查询成功");
            return "main";
        }else {
            model.addAttribute("查询失败，没有数据");
            System.out.println("查询失败，没有数据");
            return "main";
        }
    }
    @ApiOperation("根据ID修改一条数据")
    @PutMapping("/updata")
    public String updata(Comments comments , Model model){
        Integer i = commentsService.updata(comments);
        if (i > 0){
            model.addAttribute("model","修改成功");
            System.out.println("修改成功");
            return "main";
        }else {
            model.addAttribute("model","修改失败");
            System.out.println("修改失败");
            return "main";
        }
    }
}

